var apipath='api';
var test=true;
if(test){
	apipath='api';
}else{
	apipath='';
}
//=====================================================================
$.fn.formInit=function(opt){
	var o={
		id:'',
		url:'',
		submitbtn:'',
		otherdata:null,
		callback:null,
	};
	var that=this;
	var f=null;
	o=$.extend(o,opt);
	if(!o.id){
		o.id='#'+$(that).attr('id');
	}
	if(!o.url){
		o.url=$(that).attr('action');
	}
	if(!o.submitbtn){
		o.submitbtn='.submit,[type=submit]';
	}
	that._sumbmit=function(){
		if(!f.validate()){
			return false;
		}
		if(!o.url){
			return false;
		}
		var data=f.getFormData();
		data=$.extend(data,o.otherdata)
		AjaxPost(o.url,data,o.callback);
	}
	that.init=function(){
		f=new Form(o.id)
		$(that).find(o.submitbtn).off('tap').on('tap',function(){
			that._sumbmit();
		})
	}
	that.init();

}
//=====================================================================
//ajax api
function AjaxPost(url,data,callback,Async,obj){

	if(url.indexOf('http://')==-1){
		url =apipath+url;
	}
	var isasync=true;
	var relust;
	if(Async!=null)isasync=Async;
	  var getTimestamp=new Date().getTime();
	  if(url.indexOf("?")>-1){
		url=url+"&timestamp="+getTimestamp;
	  }else{
		url=url+"?timestamp="+getTimestamp;
	  }

	A.ajax({
		url:url,
		data:data,
		type:'POST',
		dataType:'json',
		async:isasync,
		error:function(textstatus,errorCode){

			if(textstatus.responseText.indexOf('<!DOCTYPE html>')>-1){
				gotoNewPage('');
			}
			console.log(url+':'+errorCode);
		},
	success:function (data, textStatus) {
		if(typeof(data)=='string'){
			if(data.indexOf('<html')>-1)
			gotoNewPage(path);
			return;
		}
		if(typeof(callback)=='function')
		{
			callback(data,obj);
		}
			relust=data;
		

	},
		setTimeout:5000
	});
	return relust;
}

function gotoNewPage(){
	console.log(arguments)
	var page=arguments[0];
	for(var i=1;i<arguments.length;i++){
		if(typeof(arguments[i])== 'object'){
			var _a=arguments[i];
			for(o in _a){
				page=addParmUrl(page,o,_a[o])
			}
		}
	}
	window.location.href=page;
}
function addParmUrl(page,n,v){
	if(page.indexOf('?')>0){
		page=page+"&"+n+'='+v;
	}else{
		page=page+"?"+n+'='+v;
	}
	return page;
}